import { defineStore } from "pinia";
import request from '@/utils/request'
import {ApiRes,CategoryItem,TopCategory,SubCategory} from '@/types/data'
import {toCategory} from '@/store/constants'
const defaultCategory = toCategory.map((item) => {
    return {
        name:item
    }
})
export default defineStore('category',{
    state() {
        return {
            list:defaultCategory as CategoryItem[],
            topCategory: {} as TopCategory,
            subCategory: {} as SubCategory,
        }
    },
    actions:{
       async getCategoryList() {
            const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
            // console.log(res.data.result)
            res.data.result.forEach(item => {
                item.open = false
            })
            this.list = res.data.result
        },
        show(id:string) {
            const obj = this.list.find((item) => item.id === id) 
            obj!.open = true
        },
        hide(id:string) {
            const obj = this.list.find((item) => item.id === id) 
            obj!.open = false
        },
        async getTopCategory(id: string) {
            const res = await request.get<ApiRes<TopCategory>>('/category', {
              params: {
                id,
              },
            })
            this.topCategory = res.data.result
            // console.log(this.topCategory)
            
          },
        async getSubFilter(id: string) {
            const res = await request.get<ApiRes<SubCategory>>(
              '/category/sub/filter',
              {
                params: {
                  id,
                },
              }
            )
            this.subCategory = res.data.result
            // console.log(this.subCategory)
            
          },
    }
})